package com.erp.framework.controller.taglib;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.jsp.JspException;

import com.erp.framework.SimpleConstants;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.util.logger.Logger;

public class CustomSelectTag extends javax.servlet.jsp.tagext.TagSupport {

    private String table = null;
    private String valuecolumn = null;
    private String textcolumn = null;
    private String where = null;
    private String order = null;
    private String selected = null;
    private String name = null;
    private String clazz = null;

    public void setTable(String text) {

        this.table = text;
    }

    public void setValuecolumn(String text) {

        this.valuecolumn = text;
    }

    public void setTextcolumn(String text) {

        this.textcolumn = text;
    }

    public void setWhere(String text) {

        this.where = text;
    }

    public void setOrder(String text) {

        this.order = text;
    }

    public void setSelected(String text) {

        this.selected = text;
    }

    public void setName(String name) {

        this.name = name;
    }

    public void setClazz(String clazz) {

        this.clazz = clazz;
    }

    public int doEndTag() throws JspException {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        StringBuffer rslt = new StringBuffer();
        String query = "";

        try {
            rslt = new StringBuffer();

            query = " SELECT " + valuecolumn + " COLA," + textcolumn + " COLB FROM " + table
                    + ((where != null && !where.equals("")) ? " WHERE " + where : "")
                    + ((order != null && !order.equals("")) ? " ORDER BY " + order : "");

            String valueStr = null;
            boolean notFound = false;

            rslt.append("<select name='" + name + "' class='" + clazz + "'>");
            rslt.append("		<option value=''></option>");

            conn = ServiceLocator.getInstance().getDSDBConnection();
            stmt = conn.createStatement();
            Logger.all(this, "doEndTag(), sql=" + query);
            rs = stmt.executeQuery(query);

            if (rs != null) {
                selected = (selected == null) ? "" : selected.trim();
                int i = 0;
                while (rs.next()) {
                    i++;
                    valueStr = (rs.getString(1) == null) ? "" : rs.getString(1).trim();
                    if (SimpleConstants.DB_NIL.equalsIgnoreCase(valueStr)) {
                        notFound = true;
                        break;
                    }

                    rslt.append("<OPTION VALUE='" + valueStr + "'");
                    if (valueStr.equals(selected))
                        rslt.append(" SELECTED");
                    rslt.append(">");
                    rslt.append((rs.getString(2) == null) ? "" : rs.getString(2));
                    rslt.append("</OPTION>\n");
                }

                if (i == 0) {
                    Logger.trace(this, "doEndTag(), NOT FOUND COLOR/SIZE");
                    notFound = true;
                }
            }

            rslt.append("</select>");

            if (notFound) {
                rslt = new StringBuffer();

                if (name != null) {
                    rslt.append("<input type=\"hidden\" name=\"");
                    rslt.append(name);
                    rslt.append("\" value=\"");
                    rslt.append(SimpleConstants.DB_NIL);
                    rslt.append("\" />");
                }
            }
            Logger.all(this, "doEndTag(), html=" + rslt.toString());
            pageContext.getOut().print(rslt.toString());
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            Logger.error("doEndTag(), html=" + rslt.toString());
            Logger.error("doEndTag(), sql=" + query);
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }

        return EVAL_PAGE;
    }
}